<template lang="pug">
    div(class="statistics")
        skin(@up="up",@down="down",@yes="yes",@back="back")
            div(class="flex-column h3")
                div(class="flex-column-item flex-row")
                    div(class="flex-row-item flex-3") {{current | moment().format('YYYY-MM-DD')}}
                    div(class="flex-row-item right") {{current | moment().format('HH:mm')}}
                div(class="flex-column-item flex-row")
                    div(class="flex-row-item flex-3") 当日已用量：
                    div(class="flex-row-item right") {{usedAmount}}U
                div(class="flex-column-item flex-row")
                    div(class="flex-row-item flex-3") 日总限制量：
                    div(class="flex-row-item right") {{dayDoseLimit.value}}U
                div(class="flex-column-item flex-row")
                    div(class="flex-row-item") {{record.date|moment().format("MMDD")}}
                    div(class="flex-row-item center") {{record.date|moment().format("HH:mm")}}
                    div(class="flex-row-item right") {{record.amount}}U
</template>

<script>
    import moment from 'moment';

    export default {
        data() {
            return {
                record: [].concat(window.dose.records).reverse()[0],
                dayDoseLimit: window.advance.dayDoseLimit,
                current: moment(),
                interval: null
            };
        },
        computed: {
            usedAmount() {
                let result = 0;
                const today = moment().format("YYYYMMDD")
                window.dose.records.forEach(item => {
                    if (moment(item.date).format("YYYYMMDD") == today) result = (result * 10 + item.amount * 10) / 10;
                })
                return result;
            }
        },
        methods: {
            up() {
                clearInterval(this.interval)
                this.$router.push('/home/mst');
            },
            down() {
                clearInterval(this.interval)
                this.$router.push('/home/mst');
            },

            back() {
                clearInterval(this.interval)
                this.$router.push('/index');
            },
            yes() {
                clearInterval(this.interval)
                this.$router.push('/index');
            },
            getCurrent() {
                this.current = window.machine.timeDiff ?
                    moment().add(-window.machine.timeDiff, 'seconds') :
                    moment();
            }
        },
        created() {
            this.getCurrent();
            this.interval = setInterval(function () {
                this.getCurrent();
            }.bind(this), 1000)
        }
    };
</script>
